package pt.iscte.pcd.accounts;

import java.util.Random;

public class Withdrawer extends Thread {

	private static final int MAX_DEP = 10;
	private static final long SLEEPTIME = 10;
	private int total;
	private Account c;

	public Withdrawer(Account c) {
		this.c = c;
	}

	public int totalAmountDeposited() {
		return total;
	}

	public void run() {
		Random r = new Random();
		try {
			while (true) {
				int a = r.nextInt(MAX_DEP);
				if (c.withdraw(a)) {
					System.out.println("Withdrew " + a);
					total += a;
				}
				sleep(SLEEPTIME);
			}
		} catch (InterruptedException e) {
		}
//		try {
//			sleep(SLEEPTIME);
//		} catch (InterruptedException e) {
//		}
		System.out.println("Withdrawer done");
	}

	public int totalAmountWithdrawn() {
		return total;
	}

}
